package com.shanghaionstar.servicepackage.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.more.entity.PackageInfo;
import com.shanghaionstar.servicepackage.model.OnstarBuyPkgModel;
import com.shanghaionstar.servicepackage.view.OnstarBuyPkgView;

import java.util.List;

import rx.functions.Action1;

/**
 * 安吉星套餐购买处理
 * Created by Yonglang Hong on 2016/8/8.
 */
public class OnstarBuyPkgPresenter extends BasePresenter {

    private OnstarBuyPkgView buyPackageView;

    private OnstarBuyPkgModel buyPackageModel;

    public OnstarBuyPkgPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        buyPackageModel = new OnstarBuyPkgModel(context);
    }

    public void setPackageView(OnstarBuyPkgView buyPackageView) {
        this.buyPackageView = buyPackageView;
    }

    public void getOnstarPackages(PackageType packageType) {
        subscription = buyPackageModel.getPackageInfos(packageType)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<PackageInfo>>() {
                    @Override
                    public void call(List<PackageInfo> infoList) {
                        if (buyPackageView != null) {
                            buyPackageView.showPackageInfos(infoList);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (buyPackageView != null && throwable != null) {
                            throwable.printStackTrace();
                            buyPackageView.showErr(throwable);
                        }
                    }
                });
    }
}
